package com.magicsoft.share.ui.my;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.just.library.AgentWeb;
import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.LoginShareBean;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.customview.dialog.DialogShare;
import com.magicsoft.share.presenter.my.LoginSharePersenter;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.utils.WxShareUtil;
import com.magicsoft.share.utils.ZxingUtils;
import com.magicsoft.share.view.my.ILoginShareView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by fujie on 2018/5/2.
 */

public class LoginShareActivity extends BaseActivity<ILoginShareView, LoginSharePersenter> implements ILoginShareView {
    //    @BindView(R.id.rl_share_login)
//    RelativeLayout rlShareLogin;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_qr_code_num)
    TextView tvQrCodeNum;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;


    private AgentWeb mAgentWeb;
    private String URL;

    @Override
    public LoginSharePersenter initPresenter() {
        return new LoginSharePersenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login_share;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        tvTitle.setText("我的推荐码");
        ibtnRight.setVisibility(View.VISIBLE);
        ibtnRight.setImageResource(R.drawable.share);
        mPresenter.loginshare();

    }

    @Override
    public void requestData() {

    }

    @Override
    public void loginShare(LoginShareBean url) {
        URL = url.getUrl();
        if (URL != null) {
//            mAgentWeb = AgentWeb.with(this)//传入Activity or Fragment
//                    .setAgentWebParent(rlShareLogin, new RelativeLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams ,第一个参数和第二个参数应该对应。
//                    .useDefaultIndicator()// 使用默认进度条
//                    .defaultProgressBarColor() // 使用默认进度条颜色
//                    .setReceivedTitleCallback(new ChromeClientCallbackManager.ReceivedTitleCallback() {
//                        @Override
//                        public void onReceivedTitle(WebView view, String title) {
//
//                        }
//                    }) //设置 Web 页面的 title 回调
//                    .createAgentWeb()
//                    .ready()
//                    .go(URL);
            Bitmap bitmap = ZxingUtils.createBitmap(url.getUrl());
            ivQrCode.setImageBitmap(bitmap);
            if (url.getQixiang_no()!= null && url.getQixiang_no().equals("null")){
                tvQrCodeNum.setText(""+url.getQixiang_no());
            }else{
                tvQrCodeNum.setText("");
            }
        }

    }

    /**
     * 退出
     */
    @OnClick(R.id.ibtn_left)
    public void ibtn_left() {
        finish();
    }

    /**
     * 分享
     */
    @OnClick(R.id.ibtn_right)
    public void ibtn_right() {
        DialogShare mDialogShare = DialogShare.newInstance();
        mDialogShare.setOnButtonClickListener(new DialogShare.OnButtonClickListener() {
            @Override
            public void shareToWechatFriend() {
                Resources res = getResources();
                Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.logo_my);
                WxShareUtil.getInstance().init(LoginShareActivity.this).sendToWxFriend(URL, "分享注册送豪礼", bmp);
            }

            @Override
            public void shareToFriendCircle() {
                Resources res = getResources();
                Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.logo_my);
                WxShareUtil.getInstance().init(LoginShareActivity.this).sendToFriendCircle(URL, "分享注册送豪礼", bmp);
            }
        });
        mDialogShare.show(getSupportFragmentManager(), "");
    }
}
